<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/管理.png" alt="管理图标">
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut"> 退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">

        <div class="toggle-button" @click="toggleCollapse">|||</div>

        <!-- 侧边栏 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router
          :default-active="activePath">
          <!-- 一级菜单         -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的摸版 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]" class="iconfont"></i>
              <!-- 文本 -->
              <span>{{ item.rightsName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
              <i class="el-icon-menu iconfont"></i>
              <span>{{ subitem.rightsName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      // 左侧菜单栏
      menuList: [],
      iconsObj: {
        '1': 'el-icon-s-platform',
        '2': 'el-icon-s-home',
        '3': 'el-icon-s-promotion',
        '4': 'el-icon-s-grid',
        '5': 'el-icon-share'
      },
      // 是否折叠。false 不折叠
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logOut() {
      // 清空token
      window.sessionStorage.clear();
      // 跳转到登录页
      this.$router.push("/login");
    },
    // 获取所有菜单
    async getMenuList() {
      // const {data, status} = await this.$http.post('asideMenu');
      const {data:res} = await this.$http.get('rights/getRightsList');

      if (!res.status) return this.$message.error(res.message);
      this.menuList = res.data.treeRights;
    },
    /*点击按钮实现菜单的折叠，展开*/
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    /* 保存菜单栏状态 */
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    }

  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #363D40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: white;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #313743;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #E9EDF1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

</style>
